//
//  JAddressController.m
//  HeadIndustry
//
//  Created by jun on 2017/9/12.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JAddressController.h"
#import "JCustomerAddressController.h" // 新增收货地址页面
#import "JAddressListView.h"

@interface JAddressController ()

@property (nonatomic, strong) NSMutableArray *addressViewArr;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation JAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super InitUI];
    self.titleLabel.text = @"管理收货地址";
    self.leftButton.hidden = NO;
    self.rightButton. hidden = YES;
    [self.leftButton setImage:[UIImage imageNamed:backBtnImg] forState:UIControlStateNormal];
    self.leftButton.frame = CGRectMake(8, self.leftButton.frame.origin.y, 30, 30);
    
    [self loadAddressListRequest];
    
    [self setConfigScrollView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(noticationAddAddress) name:NSStringFromClass([JCustomerAddressController class]) object:nil];
}

- (void)noticationAddAddress
{
    for (JAddressListView *listView in self.addressViewArr) {
        [listView removeFromSuperview];
    }
    
    [self loadAddressListRequest];
    
}

- (void)setConfigScrollView {
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, CGRectGetHeight(self.contentView.frame) - 50)];
    _scrollView.backgroundColor = kRGBColor(245, 245, 245);
    [self.contentView addSubview:_scrollView];
    
    UIButton *delSelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    delSelBtn.frame = CGRectMake(0, _scrollView.bottom, kWindowW, 50);
    delSelBtn.titleLabel.font = kFONT_TITLE(16);
    [delSelBtn setTitle:@"新增收货地址" forState:UIControlStateNormal];
    delSelBtn.backgroundColor = [UIColor redColor];
    [delSelBtn addTarget:self action:@selector(SetSelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:delSelBtn];
}

- (void)setConfigAddressListView:(NSArray *)dataArr
{
    for (int i = 0; i < dataArr.count; i++) {
        CGRect rect = CGRectMake(0, 10 + i * 130, kWindowW, 120);
        JAddressListView *listView = [[JAddressListView alloc]initWithFrame:rect addressmodel:dataArr[i]];
        [_scrollView addSubview:listView];
        [self.addressViewArr addObject:listView];
        
        listView.block = ^(NSInteger num, JAddressModel *model) {
            if (num == 1) {
                [self updataUserdefaultAddress:model];
            }
            if (num == 2) {
                JCustomerAddressController *addressvc = [[JCustomerAddressController alloc]init];
                addressvc.addressmodel = model;
                [self.navigationController pushViewController:addressvc animated:YES];
            }
            if (num == 3) {
               UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您确定取消该条地址吗？" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self delAddressRequest:model];
                }];
                
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                
                [alertVC addAction:doneAction];
                [alertVC addAction:cancelAction];
                
                [self presentViewController:alertVC animated:YES completion:nil];
                
            }
        };
    }
    
    _scrollView.contentSize = CGSizeMake(kWindowW, 10 + dataArr.count * 130);
}

// 删除
- (void)delAddressRequest:(JAddressModel *)model
{
    NSString *url = @"Address/DeleteAddress";
    [JNetManager DeleteAddressRequestUrl:url
                            AddressModel:model
                                 success:^(id response) {
                                     if ([response integerValue] == 1) {
                                         [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                                         for (JAddressListView *listView in self.addressViewArr) {
                                             
                                             if (model.Id == listView.model.Id) {
                                                 [listView removeFromSuperview];
                                             }
                                             
                                         }
                                         
                                     }
                                 } failure:^(NSString *err) {
                                     [SVProgressHUD showErrorWithStatus:@"删除地址失败"];
                                 }];
}

// 设置默认
- (void)updataUserdefaultAddress:(JAddressModel *)model
{
    NSString *url = @"Address/SetDefaultAddress";
    
    [JNetManager  SetDefaultAddressRequestUrl:url
                                 AddressModel:model
                                      success:^(id response) {
                                          if ([response integerValue] == 1) {
//                                              NSLog(@"设置成功");
                                              for (JAddressListView *listView in self.addressViewArr) {
                                                  
                                                  listView.setDefaultbtn.selected = NO;
                                                  
                                                  if (model.Id == listView.model.Id) {
                                                      listView.setDefaultbtn.selected = YES;
                                                  }
                                                  
                                              }
                                              
                                              
                                          }
                                      } failure:^(NSString *err) {
                                          [SVProgressHUD showErrorWithStatus:@"设置默认地址失败"];
                                      }];
}

// 获取列表
- (void)loadAddressListRequest {
    
    NSString *url = @"Address/GetAddressForCustomer";
    
    [JNetManager getCustomerAddressListRequestUrl:url
                                          success:^(id response) {
                                              if (response) {
                                                  NSArray *dataArr = response;
                                                  if (dataArr.count > 0) {
                                                      [self setConfigAddressListView:dataArr];
                                                  }
                                                  else
                                                  {
                                                      JCustomerAddressController *addressvc = [[JCustomerAddressController alloc]init];
                                                      [self.navigationController pushViewController:addressvc animated:YES];
                                                  }
                                              }
                                          } failure:^(NSString *err) {
                                              [SVProgressHUD showErrorWithStatus:err];
                                          }];
}

- (NSMutableArray *)addressViewArr
{
    if (_addressViewArr == nil) {
        _addressViewArr = [NSMutableArray array];
        
    }
    return _addressViewArr;
}

- (void)SetSelBtnClick:(UIButton *)btn
{
    JCustomerAddressController *addressvc = [[JCustomerAddressController alloc]init];
    [self.navigationController pushViewController:addressvc animated:YES];
}

- (void)leftButtonClick:(UIButton *)leftButton
{
    [super leftButtonClick:leftButton];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
